package view;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;

import model.complaintModel;
import model.userModel;

import com.toedter.calendar.JDateChooser;
import common.loadData;

import dao.complaintdao;
import dao.encrypt;
import dao.userdao;

public class adduser extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel lbaddUser = null;
	private JPanel panel_acount = null;
	private JPanel panel_info = null;
	private JLabel lbUser = null;
	private JLabel lbPass = null;
	private JLabel lbRole = null;
	private JLabel lbStatus = null;
	private JTextField txtuser = null;
	private JPasswordField txtpass = null;
	private JComboBox cmxRole = null;
	private JRadioButton radioLock = null;
	private JRadioButton radioUnlock = null;
	private JLabel lbLastname = null;
	private JLabel lbFirstname = null;
	private JLabel lbSex = null;
	private JLabel lbBirth = null;
	private JLabel lbAdress = null;
	private JTextField txtName = null;
	private JTextField txtLname = null;
	private JRadioButton radioMale = null;
	private JRadioButton radioFemale = null;
	private JDateChooser txtDate;
	private JTextField txtAddress = null;
	private JLabel lbicon = null;
	private JPanel panelTitle = null;
	private JButton btnOK = null;
	private JButton btnCancel = null;
	private ButtonGroup group=null;  


	public adduser() {
		super();
		initialize();
		loadDataByID();
	}

	private void initialize() {
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/image/management.png")));
		this.setBounds(new Rectangle(500, 100, 443, 560));
		this.setName("addUser");
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Add User");
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbicon = new JLabel();
			lbicon.setBounds(new Rectangle(107, 1, 49, 47));
			lbicon.setIcon(new ImageIcon(getClass().getResource("/image/user.png")));
			lbicon.setText("");
			lbaddUser = new JLabel();
			lbaddUser.setBounds(new Rectangle(168, 10, 109, 30));
			lbaddUser.setFont(new Font("Dialog", Font.BOLD, 24));
			lbaddUser.setForeground(new Color(26, 127, 131));
			lbaddUser.setHorizontalAlignment(SwingConstants.CENTER);
			lbaddUser.setText("Add User");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lbaddUser, null);
			jContentPane.add(getPanel_acount(), null);
			jContentPane.add(getPanel_info(), null);
			jContentPane.add(lbicon, null);
			jContentPane.add(getPanelTitle(), null);
			jContentPane.add(getBtnOK(), null);
			jContentPane.add(getBtnCancel(), null);
		}
		return jContentPane;
	}

	private JPanel getPanel_acount() {
		if (panel_acount == null) {
			lbStatus = new JLabel();
			lbStatus.setBounds(new Rectangle(30, 135, 74, 22));
			lbStatus.setText("Status:");
			lbRole = new JLabel();
			lbRole.setBounds(new Rectangle(31, 101, 73, 22));
			lbRole.setText("Role:");
			lbPass = new JLabel();
			lbPass.setBounds(new Rectangle(30, 65, 74, 21));
			lbPass.setText("Password:");
			lbUser = new JLabel();
			lbUser.setBounds(new Rectangle(30, 27, 75, 23));
			lbUser.setText("Username:");
			panel_acount = new JPanel();
			panel_acount.setLayout(null);
			panel_acount.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(153, 153, 255), 2), "Account", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.TOP, new Font("Dialog", Font.BOLD, 12), Color.blue));
			panel_acount.setForeground(new Color(51, 51, 51));
			panel_acount.setSize(new Dimension(378, 173));
			panel_acount.setLocation(new Point(27, 54));
			panel_acount.add(lbUser, null);
			panel_acount.add(lbPass, null);
			panel_acount.add(lbRole, null);
			panel_acount.add(lbStatus, null);
			panel_acount.add(getTxtuser(), null);
			panel_acount.add(getTxtpass(), null);
			panel_acount.add(getCmxRole(), null);
			panel_acount.add(getRadioLock(), null);
			panel_acount.add(getRadioUnlock(), null);
			group = new ButtonGroup();
			group.add(getRadioLock());
			group.add(getRadioUnlock());
		}
		return panel_acount;
	}

	private JPanel getPanel_info() {
		if (panel_info == null) {
			lbAdress = new JLabel();
			lbAdress.setText("Address:");
			lbAdress.setSize(new Dimension(75, 22));
			lbAdress.setLocation(new Point(30, 164));
			lbBirth = new JLabel();
			lbBirth.setText("Birth of Date:");
			lbBirth.setSize(new Dimension(75, 22));
			lbBirth.setLocation(new Point(31, 128));
			lbSex = new JLabel();
			lbSex.setBounds(new Rectangle(29, 96, 75, 22));
			lbSex.setText("Sex:");
			lbFirstname = new JLabel();
			lbFirstname.setBounds(new Rectangle(31, 26, 74, 24));
			lbFirstname.setText("First Name:");
			lbLastname = new JLabel();
			lbLastname.setBounds(new Rectangle(29, 62, 77, 22));
			lbLastname.setText("Last Name:");
			panel_info = new JPanel();
			panel_info.setLayout(null);
			panel_info.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(153, 153, 255), 2), "Infomation", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.blue));
			panel_info.setSize(new Dimension(376, 236));
			panel_info.setLocation(new Point(28, 247));
			panel_info.add(lbLastname, null);
			panel_info.add(lbFirstname, null);
			panel_info.add(lbSex, null);
			panel_info.add(lbBirth, null);
			panel_info.add(lbAdress, null);
			panel_info.add(getTxtName(), null);
			panel_info.add(getTxtLname(), null);
			panel_info.add(getRadioMale(), null);
			panel_info.add(getRadioFemale(), null);
			group = new ButtonGroup();
			group.add(getRadioMale());
			group.add(getRadioFemale());
			panel_info.add(getTxtDate(), null);
			panel_info.add(getTxtAddress(), null);
		}
		return panel_info;
	}

	private JTextField getTxtuser() {
		if (txtuser == null) {
			txtuser = new JTextField();
			txtuser.setBounds(new Rectangle(135, 23, 197, 27));
		}
		return txtuser;
	}

	private JPasswordField getTxtpass() {
		if (txtpass == null) {
			txtpass = new JPasswordField();
			txtpass.setLocation(new Point(134, 64));
			txtpass.setSize(new Dimension(197, 27));
		}
		return txtpass;
	}

	private JComboBox getCmxRole() {
		if (cmxRole == null) {
			cmxRole = new JComboBox();
			cmxRole.setBounds(new Rectangle(136, 101, 120, 26));
			cmxRole.addItem("Admin");
			cmxRole.addItem("User");
			cmxRole.setSelectedIndex(1);
		}
		return cmxRole;
	}

	private JRadioButton getRadioLock() {
		if (radioLock == null) {
			radioLock = new JRadioButton();
			radioLock.setBounds(new Rectangle(135, 137, 89, 25));
			radioLock.setText("Lock");
		}
		return radioLock;
	}

	private JRadioButton getRadioUnlock() {
		if (radioUnlock == null) {
			radioUnlock = new JRadioButton();
			radioUnlock.setBounds(new Rectangle(224, 139, 99, 23));
			radioUnlock.setText("Unlock");
		}
		return radioUnlock;
	}

	private JTextField getTxtName() {
		if (txtName == null) {
			txtName = new JTextField();
			txtName.setLocation(new Point(132, 21));
			txtName.setSize(new Dimension(197, 27));
		}
		return txtName;
	}

	private JTextField getTxtLname() {
		if (txtLname == null) {
			txtLname = new JTextField();
			txtLname.setLocation(new Point(132, 60));
			txtLname.setSize(new Dimension(197, 27));
		}
		return txtLname;
	}

	private JRadioButton getRadioMale() {
		if (radioMale == null) {
			radioMale = new JRadioButton();
			radioMale.setBounds(new Rectangle(131, 96, 93, 22));
			radioMale.setText("Male");
		}
		return radioMale;
	}

	private JRadioButton getRadioFemale() {
		if (radioFemale == null) {
			radioFemale = new JRadioButton();
			radioFemale.setBounds(new Rectangle(224, 94, 104, 25));
			radioFemale.setText("Female");
		}
		return radioFemale;
	}

	private JDateChooser getTxtDate() {
		if (txtDate == null) {
			txtDate = new JDateChooser();
			txtDate.setDateFormatString("MM/dd/yyyy");
			txtDate.setLocation(new Point(134, 124));
			txtDate.setSize(new Dimension(197, 27));
			txtDate.getDateEditor().setEnabled(false);
		}
		return txtDate;
	}

	private JTextField getTxtAddress() {
		if (txtAddress == null) {
			txtAddress = new JTextField();
			txtAddress.setLocation(new Point(134, 163));
			txtAddress.setSize(new Dimension(197, 27));
		}
		return txtAddress;
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBounds(new Rectangle(166, 9, 112, 31));
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		}
		return panelTitle;
	}

	private JButton getBtnOK() {
		if (btnOK == null) {
			btnOK = new JButton();
			btnOK.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnOK.setIcon(new ImageIcon(getClass().getResource("/image/OK.png")));
			btnOK.setText("Add");
			btnOK.setLocation(new Point(316, 497));
			btnOK.setSize(new Dimension(88, 28));
			btnOK.setHorizontalAlignment(SwingConstants.LEFT);
			btnOK.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// validate
					 Pattern username = Pattern.compile("^[a-z0-9_-]{3,15}$");
				     Matcher mu = username.matcher(txtuser.getText());
				     boolean resultu = mu.find();
				     //pass validate
				     Pattern pa = Pattern.compile("^[a-z0-9]{6,20}$");
				     Matcher mp = pa.matcher(txtpass.getText());
				     boolean resultp = mp.find();
				     //name
				     Pattern pname = Pattern.compile("^[a-z ]{3,20}$");
				     Matcher name = pname.matcher(txtName.getText());
				     boolean resultfn = name.find();
				     Matcher lname = pname.matcher(txtLname.getText());
				     boolean resultln = lname.find();
				     //address
				     Pattern paddress = Pattern.compile("^[a-zA-Z0-9 ]{5,100}$");
				     Matcher adress = pname.matcher(txtAddress.getText());
				     boolean resultadd = adress.find();
				     //date
				     if(txtuser.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter UserName!");
				     }
				     else if(resultu==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "UserName wrong!");
				     }
				     else if(txtpass.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter PassWord!");
				     }
				     else if(resultp==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "PassWord wrong!");
				     }
				     else if(!radioLock.isSelected()&&!radioUnlock.isSelected())
				     {
				    	 JOptionPane.showMessageDialog(null, "Choice status for user!");
				     }
				     else if(txtName.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter first name!");
				     }
				     else if(resultfn==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "First name wrong!");
				     }
				     else if(txtLname.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter last name!");
				     }
				     else if(resultln==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "Last name wrong!");
				     }
				     else if(!radioMale.isSelected()&&!radioFemale.isSelected())
				     {
				    	 JOptionPane.showMessageDialog(null, "Choice sex for user!");
				     }
				     else if(txtDate.getDate()==null)
				     {
				    	 JOptionPane.showMessageDialog(null, "Choice birth for user!");
				     }
				     else if(txtAddress.getText().equals(""))
				     {
				    	 JOptionPane.showMessageDialog(null, "Enter adress of user!"); 
				     }
				     else if(resultadd==false)
				     {
				    	 JOptionPane.showMessageDialog(null, "Address wrong!"); 
				     }
				     else 
				     {
				    	 userModel model = new userModel();
							if(panelmanageuser.UserId>=0)
							{
								model.setId(panelmanageuser.UserId);
							}
							model.setIdrole(cmxRole.getSelectedIndex());
							model.setUserName(txtuser.getText());
							model.setPassWprd(encrypt.maHoaPw(txtpass.getText()));
							model.setFirstName(txtName.getText());
							model.setLastName(txtLname.getText());
							if(radioMale.isSelected())
							{
								model.setSex(1);
							}
							else
							{
								model.setSex(0);
							}
							SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
							String da = sdf.format(txtDate.getDate());
							model.setBirth_date(da);
							model.setAddress(txtAddress.getText());
							if(radioLock.isSelected())
							{
								model.setStatus(0);
							}
							else
							{
								model.setStatus(1);
							}
							java.util.Date now = new java.util.Date();
							String d = sdf.format(now);
							model.setLast_update(d);
							// process add User
							if(panelmanageuser.UserId>=0)
							{
								int rs = userdao.updateuser(model);
								if(rs==1)
								{
									JOptionPane.showMessageDialog(null, "Update sucessful!");
									loadData.loadDataTableUser();
									loadData.tab=1;
									
								}
								else
								{
									JOptionPane.showMessageDialog(null, "Update failed!");
								}
							}
							else
								{
									int rs = userdao.InsertUser(model);
									if(rs==1)
									{
										JOptionPane.showMessageDialog(null, "Insert sucessful!");
										loadData.loadDataTableUser();
										loadData.tab=1;
						
									}
									else
									{
										JOptionPane.showMessageDialog(null, "Insert failed!");
									}
								}
							mainForm a = new mainForm();
							a.setVisible(true);
							dispose();
				     }
					
				}	
			});
		}
		return btnOK;
	}

	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnCancel.setIcon(new ImageIcon(getClass().getResource("/image/Close.png")));
			btnCancel.setText("Exit");
			btnCancel.setLocation(new Point(220, 497));
			btnCancel.setSize(new Dimension(84, 28));
			btnCancel.setPreferredSize(new Dimension(84, 34));
			btnCancel.setHorizontalAlignment(SwingConstants.LEFT);
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					loadData.tab=1;
					mainForm a = new mainForm();
					a.setVisible(true);
					dispose();
				}
			});
		}
		return btnCancel;
	}
	private void loadDataByID()
	{
		if(panelmanageuser.UserId>=0)
		{
		lbaddUser.setText("Update User");
		lbaddUser.setSize(new Dimension(140, 28));
		panelTitle.setSize(new Dimension(140, 28));
		btnOK.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
		btnOK.setText("Save");
		btnOK.setSize(new Dimension(90, 28));
		ArrayList<userModel> user = userdao.getdatauser(panelmanageuser.UserId);
		for(userModel model : user)
		{
			txtuser.setText(model.getUserName());
			txtpass.setText(model.getPassWprd());
			if(model.getStatus()==1)
			{
				radioUnlock.setSelected(true);
			}
			else
			{
				radioLock.setSelected(true);
			}
			txtName.setText(model.getFirstName());
			txtLname.setText(model.getLastName());
			try{
			SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
			txtDate.setDate(sdf.parse(model.getBirth_date()));
			}catch (Exception e) {
				e.printStackTrace();
			}
			txtAddress.setText(model.getAddress());
			if(model.getSex()==1)
			{
				radioMale.setSelected(true);
			}
			else
			{
				radioFemale.setSelected(true);
			}
			if(model.getIdrole()==0)
			{
				cmxRole.setSelectedIndex(0);
			}
			cmxRole.setSelectedIndex(1);
		
		}
	}
	}


}
